package com.henghai.zhenxuan.ui.page.mine;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.OrderInfoBean;
import com.henghai.zhenxuan.domain.request.ProductOrderInfoRequester;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

public class OrderInfoFragment extends BaseFragment {
    private CommodityStates mStates;
    private ProductOrderInfoRequester mProductInfoRequester;

    public static final String ORDER_KEY = "orderId";


    @Override
    protected void initViewModel() {
        mProductInfoRequester = getFragmentScopeViewModel(ProductOrderInfoRequester.class);
        mStates = getFragmentScopeViewModel(CommodityStates.class);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        int orderId = bundle.getInt(ORDER_KEY);
        WaitDialog dialog = WaitDialog.show("正在加载");
        mProductInfoRequester.getProductInfoResult().observe(this, httpBeanDataResult -> {
            if (dialog.isShow()) {
                WaitDialog.dismiss();
            }
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }

            mStates.orderInfo.set(httpBeanDataResult.getResult().getData());
        });
        mProductInfoRequester.requestProductInfo(this, orderId);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_order_info, BR.vm, mStates).addBindingParam(BR.listener,
                new ListenerHandler()).addBindingParam(BR.click, new OrderInfoFragment.ClickProxy());
    }

    public class ClickProxy {
        public void address() {

        }
    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    public static class CommodityStates extends StateHolder {
        public final State<OrderInfoBean> orderInfo = new State<>(new OrderInfoBean());
    }
}
